# _*_ coding:utf-8 _*_

from django import template

from movie.models import Review
from post.models import ReviewComment
register = template.Library()


@register.simple_tag
def auto_print(model, *args):
	if isinstance(model, ReviewComment):
		r_comment = model
		for arg in args:
			if arg == "avatar":
				return r_comment.user.avatar
			elif arg == "name":
				return r_comment.user_username
			elif arg == "timestamp":
				return r_comment.timestamp.strftime("%Y-%m-%d")
			elif arg == 'content':
				return r_comment.content
		return None


def get_praise(review, uid):
	praise = review.reviewpraise_set.filter(user_id=uid)
	return praise[0] if praise else None


def get_status(review, uid):
	praise = review.reviewpraise_set.filter(user_id=uid)
	if praise and praise[0].status == True:
		return True
	return False


@register.filter
def get_r_praise(review, uid):
	pid = get_praise(review, uid).id if get_status(review, uid) else ''
	return pid


@register.filter
def get_r_status(review, uid):
	status = "1" if get_status(review, uid) else "0"
	return status


@register.filter
def get_r_comments(review):
	counts = review.reviewcomment_set.count()
	return counts